//
//  MapViewController.m
//  MeiShiDa
//
//  Created by zby on 15/6/30.
//  Copyright (c) 2015年 zby. All rights reserved.
//

#import "MapViewController.h"
#import "MapDetailModel.h"
@interface MapViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>
@property (nonatomic,strong) NSMutableArray* arr;
@property (nonatomic,strong) CLLocationManager* locationManager;
@property (nonatomic,strong) CLGeocoder* geocoder;
@property (nonatomic,strong) MKMapView* mapView;
@property (nonatomic,strong) CLLocation* userLocation;
@end

@implementation MapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    [self getFakeData];
    self.locationManager = [[CLLocationManager alloc] init];
    self.geocoder = [[CLGeocoder alloc] init];
    self.userLocation = [[CLLocation alloc] init];
    [self setUserLocationManager];
}
-(void)initGUI{
    CGRect rect = [[UIScreen mainScreen] bounds];
    self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, rect.size.width, rect.size.height-49)];
    self.mapView.delegate = self;
    self.mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
    self.mapView.mapType = MKMapTypeStandard;
    [self.view addSubview:self.mapView];
    [self addAnnotation];
}
-(void)addAnnotation{
    for (MapDetailModel* abc in self.arr) {
        MKPointAnnotation* userAnnotation = [[MKPointAnnotation alloc] init];
        userAnnotation.title = abc.title;
        userAnnotation.subtitle = abc.descr;
        CLLocationCoordinate2D coordinate2D = self.userLocation.coordinate;
        int temp = arc4random()%10;
        userAnnotation.coordinate = CLLocationCoordinate2DMake(coordinate2D.latitude + temp*0.001, coordinate2D.longitude + temp*0.001);
        [self.mapView addAnnotation:userAnnotation];
    }
}

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    if ([annotation isKindOfClass:[MKPointAnnotation class]]) {
        static NSString* identifier = @"doby";
        MKAnnotationView* annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (annotationView == nil) {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
            annotationView.canShowCallout = true;
            annotationView.leftCalloutAccessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"shicai.png"]];
        }
        annotationView.annotation = annotation;
        annotationView.image = [UIImage imageNamed:@"ditu.png"];
        return annotationView;
    }else{
        return nil;
    }
    
}

-(void)getFakeData{
    
    MapDetailModel* model1 = [[MapDetailModel alloc] init];
    model1.title = @"土大力";
    model1.descr = @"玉米芝士";
    MapDetailModel* model2 = [[MapDetailModel alloc] init];
    model2.title = @"肯德基";
    model2.descr = @"肯德基豪华套餐";
    MapDetailModel* model3 = [[MapDetailModel alloc] init];
    model3.title = @"麦当劳";
    model3.descr = @"英格兰打卤面";
    MapDetailModel* model4 = [[MapDetailModel alloc] init];
    model4.title = @"川湘情";
    model4.descr = @"带皮口味蛇";
    MapDetailModel* model5 = [[MapDetailModel alloc] init];
    model5.title = @"阳光小厨";
    model5.descr = @"蚂蚁上树";
    MapDetailModel* model6 = [[MapDetailModel alloc] init];
    model6.title = @"肯打鸡";
    model6.descr = @"唐马儒鉴黄套餐";
    [self.arr addObject:model1];
    [self.arr addObject:model2];
    [self.arr addObject:model3];
    [self.arr addObject:model4];
    [self.arr addObject:model5];
    [self.arr addObject:model6];
}
-(NSMutableArray*)arr{
    if (_arr == nil) {
        _arr = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _arr;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)setUserLocationManager{
    if (![CLLocationManager locationServicesEnabled]) {
        return;
    }
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
        [self.locationManager requestWhenInUseAuthorization];
    }
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse)
    {
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        self.locationManager.distanceFilter = 10.0;
        [self.locationManager startUpdatingLocation];
    }
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    
    CLLocation* location = [locations firstObject];
    self.userLocation = location;
//    CLLocationCoordinate2D coordinate = location.coordinate;
//    NSLog(@"经度%f，维度%f",coordinate.longitude,coordinate.latitude);
//    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
//        CLPlacemark* firstPlaceMark = [placemarks firstObject];
//        
//        NSLog(@"%@",firstPlaceMark.name);
//        
//    }];
    
    [self.locationManager stopUpdatingLocation];
    [self initGUI];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
